package br.com.santana.route;

import br.com.maplink2.webservices.Address;
import br.com.maplink2.webservices.AddressFinder;
import br.com.maplink2.webservices.AddressFinderSoap;
import br.com.maplink2.webservices.ArrayOfRouteStop;
import br.com.maplink2.webservices.Point;
import br.com.maplink2.webservices.Route;
import br.com.maplink2.webservices.RouteDetails;
import br.com.maplink2.webservices.RouteOptions;
import br.com.maplink2.webservices.RouteSoap;
import br.com.maplink2.webservices.RouteStop;
import br.com.maplink2.webservices.RouteTotals;
import br.com.maplink2.webservices.Vehicle;
import br.com.santana.route.to.RouteTO;

public class RouteImpl implements IRoute {

	final String TOKEN = "c13iyCvmcC9mzwkLd0LCbmYC5mUF5m2jNGNtNGt6NmK6NJK=";
	
	@Override
	public Point getCoordinatesByAddress(Address address) {
		AddressFinder service1 = new AddressFinder();
        AddressFinderSoap port1 = service1.getAddressFinderSoap12();
        
		Point pointOrigin = port1.getXY(address, TOKEN);
		return pointOrigin;
	}

	@Override
	public RouteTO getRouteValues(RouteTO routTO) {		
		
		RouteStop originRoute = new RouteStop();
		originRoute.setDescription("Begin");
		originRoute.setPoint(routTO.getOrigin());		 
		
		RouteStop destinationRoute = new RouteStop();
		destinationRoute.setDescription("End");
		destinationRoute.setPoint(routTO.getDestination());
		 
		ArrayOfRouteStop list = new ArrayOfRouteStop();
		list.getRouteStop().add(originRoute);
		list.getRouteStop().add(destinationRoute);		
	 
		RouteDetails routeDetails = new RouteDetails();
		routeDetails.setDescriptionType(0);
		routeDetails.setRouteType(1);
		routeDetails.setOptimizeRoute(true);
		 
		Vehicle vehicle = new Vehicle();
		vehicle.setTankCapacity(20);
		vehicle.setAverageConsumption(9);
		vehicle.setFuelPrice(3);
		vehicle.setAverageSpeed(60);
		vehicle.setTollFeeCat(2);
		 
		RouteOptions routeOptions = new RouteOptions();
		routeOptions.setLanguage("portugues");
		routeOptions.setRouteDetails(routeDetails);
		routeOptions.setVehicle(vehicle);
		 
		Route routeLocator = new Route();
		RouteSoap route = routeLocator.getRouteSoap12();
		RouteTotals getRouteTotalsResponse = route.getRouteTotals(list, routeOptions, TOKEN);
		
		routTO.setTotalDistance(getRouteTotalsResponse.getTotalDistance());
		routTO.setTotalFuel(getRouteTotalsResponse.getTotalfuelCost());
		routTO.setTotalTime(getRouteTotalsResponse.getTotalTime());
		routTO.setTotalToll(getRouteTotalsResponse.getTotaltollFeeCost());		
				
		return routTO;
		
	}

}
